﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;

using Jungle;
using Jungle.Code.Utils;
using Jungle.Code.Core;
using Jungle.Code.BLL;
using Jungle.Code.DAL;
using Jungle.Blog;
using Jungle.Forum;

public partial class admin : BaseAdminModule
{
    public int US_ID;
    string NickName = "";

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
       
       InitializeComponent();
        base.OnInit(e);
    }
    //--------------------------------------------------------------
    private void InitializeComponent()
    {
        this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
        this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
        this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
        this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
        this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
        this.DataGrid1.ItemCommand += new DataGridCommandEventHandler(this.Item_Command);
        this.DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(this.DataGrid1_Change);

        //----------------------
        this.DataGrid2.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid2_CancelCommand);
        this.DataGrid2.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid2_EditCommand);
        this.DataGrid2.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid2_UpdateCommand);
        this.DataGrid2.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid2_DeleteCommand);
        this.DataGrid2.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid2_ItemDataBound);
        this.DataGrid2.PageIndexChanged += new DataGridPageChangedEventHandler(this.DataGrid2_Change);
       
        //----------------------
        this.DataGrid3.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid3_CancelCommand);
        this.DataGrid3.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid3_EditCommand);
        this.DataGrid3.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid3_UpdateCommand);
        this.DataGrid3.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid3_DeleteCommand);
        this.DataGrid3.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid3_ItemDataBound);
        this.DataGrid3.PageIndexChanged += new DataGridPageChangedEventHandler(this.DataGrid3_Change);
    }
    #endregion

   //-------------------------------------------------------------------------------------
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
         
           bindData();
           fill();
           fillDropDown();
           fillDropDown1();
       }
   }
     //-------------------------------------------------------------------------------------
   private void fillDropDown1()
   {
       //--------
       UserManager um = new Jungle.Blog.UserManager();
       DataSet ds2 = new DataSet();
       ds2 = um.GetRoles();
       Label7.Text = um.err;

       DropDownDRoles1.Items.Clear();
       DropDownDRoles1.DataSource = ds2.Tables[0].DefaultView;
       DropDownDRoles1.DataTextField = "descript";        //(то, что видит юзер)
       DropDownDRoles1.DataValueField = "RoleID";      //(value значения в <option>) 
       DropDownDRoles1.DataBind();
       ListItem NewItemU = new ListItem();
       NewItemU.Text = "-- Выберите роль --";
       NewItemU.Value = "-1";
       NewItemU.Selected = true;
       DropDownDRoles1.Items.Add(NewItemU);
   }
   //-------------------------------------------------------------------------------------
   private void fillDropDown()
   {
       ForumCategory fc = new Jungle.Forum.ForumCategory();
       DataSet ds = new DataSet();
       ds = fc.GetAllCategory();
       Label7.Text = fc.err;

       DropDownDForumCategory.Items.Clear();
       DropDownDForumCategory.DataSource = ds.Tables[0].DefaultView;
       DropDownDForumCategory.DataTextField = "title";        //(то, что видит юзер)
       DropDownDForumCategory.DataValueField = "ForumCategoryID";      //(value значения в <option>) 
       DropDownDForumCategory.DataBind();
       ListItem NewItem = new ListItem();
       NewItem.Text = "-- Выберите категорию --";
       NewItem.Value = "-1";
       NewItem.Selected = true;
       DropDownDForumCategory.Items.Add(NewItem);
     

       //-------
       ForumVirt fv = new Jungle.Forum.ForumVirt();
       DataSet ds1 = new DataSet();
       fv.GetAllForumVirt();
       ds1 = fv.ds;
       Label7.Text = fv.err;

       DropDownDForum.Items.Clear();
       DropDownDForum.DataSource = ds1.Tables[0].DefaultView;
       DropDownDForum.DataTextField = "title";        //(то, что видит юзер)
       DropDownDForum.DataValueField = "ForumId";      //(value значения в <option>) 
       DropDownDForum.DataBind();
       ListItem NewItemZ = new ListItem();
       NewItemZ.Text = "-- Выберите Форум --";
       NewItemZ.Value = "-1";
       NewItemZ.Selected = true;
       DropDownDForum.Items.Add(NewItemZ);
      
       //--------
       UserManager um = new Jungle.Blog.UserManager();
       DataSet ds2 = new DataSet();
       ds2 = um.GetRoles();
       Label7.Text = um.err;

       DropDownDRoles.Items.Clear();
       DropDownDRoles.DataSource = ds2.Tables[0].DefaultView;
       DropDownDRoles.DataTextField = "descript";        //(то, что видит юзер)
       DropDownDRoles.DataValueField = "RoleID";      //(value значения в <option>) 
       DropDownDRoles.DataBind();
       ListItem NewItemU = new ListItem();
       NewItemU.Text = "-- Выберите роль --";
       NewItemU.Value = "-1";
       NewItemU.Selected = true;
       DropDownDRoles.Items.Add(NewItemU);

   }
   //-------------------------------------------------------------------------------------
   private void fill()
   {
       // невидимые
       ForumCategory fc = new Jungle.Forum.ForumCategory();
       DataSet ds = new DataSet();
       ds=fc.GetAllCategory();
       Label7.Text = fc.err;
     
       DropDownForumCategory.Items.Clear();
       DropDownForumCategory.DataSource = ds.Tables[0].DefaultView;
       DropDownForumCategory.DataTextField = "title";        //(то, что видит юзер)
       DropDownForumCategory.DataValueField = "ForumCategoryID";      //(value значения в <option>) 
       DropDownForumCategory.DataBind();

       ListItem NewItem = new ListItem();
       NewItem.Text = "";
       NewItem.Value = "-1";
       DropDownForumCategory.Items.Add(NewItem);
    
       //--------
       ForumVirt fv = new Jungle.Forum.ForumVirt();
       DataSet ds1 = new DataSet();
       fv.GetAllForumVirt();
       ds1 = fv.ds;
       Label7.Text = fv.err;
       
       DropDownForum.Items.Clear();
       DropDownForum.DataSource = ds1.Tables[0].DefaultView;
       DropDownForum.DataTextField = "title";        //(то, что видит юзер)
       DropDownForum.DataValueField = "ForumId";      //(value значения в <option>) 
       DropDownForum.DataBind();
       ListItem NewItemZ = new ListItem();
       NewItemZ.Text = "";
       NewItemZ.Value = "-1";
       DropDownForum.Items.Add(NewItemZ);
    
       //--------
       UserManager um = new Jungle.Blog.UserManager();
       DataSet ds2 = new DataSet();
       ds2=um.GetRoles();
       Label7.Text = um.err;
     
       DropDownUsRoles.Items.Clear();
       DropDownUsRoles.DataSource = ds2.Tables[0].DefaultView;
       DropDownUsRoles.DataTextField = "descript";        //(то, что видит юзер)
       DropDownUsRoles.DataValueField = "RoleID";      //(value значения в <option>) 
       DropDownUsRoles.DataBind();

       ListItem NewItemU = new ListItem();
       NewItemU.Text = "";
       NewItemU.Value = "-1";
       DropDownUsRoles.Items.Add(NewItemU);
       //--------
     
       DropDownUsRoles1.Items.Clear();
       DropDownUsRoles1.DataSource = ds2.Tables[0].DefaultView;
       DropDownUsRoles1.DataTextField = "descript";        //(то, что видит юзер)
       DropDownUsRoles1.DataValueField = "RoleID";      //(value значения в <option>) 
       DropDownUsRoles1.DataBind();

       ListItem NewItemR = new ListItem();
       NewItemR.Text = "";
       NewItemR.Value = "-1";
       DropDownUsRoles1.Items.Add(NewItemR);
   }
   //-------------------------------------------------------------------------------------
   private void bindData()
   {
       UserManager usr = new UserManager();
       DataSet ds2 = new DataSet();
       usr.GetAllUsers();
       ds2 = usr.ds;
       Label1.Text = usr.err;

       DataGrid1.DataSource = ds2.Tables[0].DefaultView;
       DataGrid1.DataKeyField = "userid";
       DataGrid1.DataBind();
   }
    //-------------------------------------------------------------------------------------
   private void bindData1()
   {
       ForumCategory cts = new ForumCategory();
       //if (Session["uid"] != null) cts.r_UserID = Convert.ToInt32(Session["uid"]);
       cts.r_UserID = Convert.ToInt32(lb_userId.Text);
       cts.GetRoleByUserId();
       Label5.Text = cts.err;

       DataGrid2.DataSource = cts.r_ds.Tables[0].DefaultView;
       DataGrid2.DataKeyField = "UserRolesID";
       DataGrid2.DataBind();
   }
   //-------------------------------------------------------------------------------------
   private void bindData2()
   {
       Post pst = new Post();
       //if (Session["uid"] != null) pst.r_UserID = Convert.ToInt32(Session["uid"]);
       pst.r_UserID = Convert.ToInt32(lb_userId.Text);
       pst.GetRoleByUserId();
       Label6.Text = pst.err;

       DataGrid3.DataSource = pst.r_ds.Tables[0].DefaultView;
       DataGrid3.DataKeyField = "UserRolesID";
       DataGrid3.DataBind();
   }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        UserManager usr = new UserManager();
        usr.UserId = Convert.ToInt16(DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
        usr.Delete();
        Label1.Text = usr.err;

        DataGrid1.EditItemIndex = -1;
        bindData();
    }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid2_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        ForumCategory cts = new ForumCategory();
        cts.r_UserRolesID = Convert.ToInt16(DataGrid2.DataKeys[e.Item.ItemIndex].ToString());
        cts.r_Delete();
        Label1.Text = cts.err;

        DataGrid2.EditItemIndex = -1;
        bindData1();
    }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid3_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        Post pst = new Post();
        pst.r_UserRolesID = Convert.ToInt16(DataGrid3.DataKeys[e.Item.ItemIndex].ToString());
        pst.r_Delete();
        Label1.Text = pst.err;

        DataGrid3.EditItemIndex = -1;
        bindData2();
    }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    // DataGrid1.SelectedIndex = e.Item.ItemIndex;
     DataGrid1.EditItemIndex = e.Item.ItemIndex;
     bindData();
    }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid2_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
      
        Button2.Visible = false; Button4.Visible = false;
        ForumCategory cts = new ForumCategory();
        cts.r_UserRolesID = Convert.ToInt16(DataGrid2.DataKeys[e.Item.ItemIndex].ToString());

        cts.GetByRoleId();
        Label5.Text = cts.err;

        DropDownDRoles.SelectedValue = Convert.ToString(cts.r_RoleValue);
        DropDownDForum.SelectedValue = Convert.ToString(cts.r_ForumVirtID);
        DropDownDForumCategory.SelectedValue = Convert.ToString(cts.r_ForumCategoryID);

        DataGrid2.EditItemIndex = e.Item.ItemIndex;
        bindData1();
    }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid3_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        Button5.Visible = false; Button6.Visible = false;
        Post bp = new Post();
        bp.r_UserRolesID = Convert.ToInt16(DataGrid3.DataKeys[e.Item.ItemIndex].ToString());

        bp.GetByRoleId();
        Label6.Text = bp.err;

        DropDownDRoles1.SelectedValue = Convert.ToString(bp.r_RoleValue);

        DataGrid3.EditItemIndex = e.Item.ItemIndex;
        bindData2();
    }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
     UserManager usr = new UserManager();
     usr.UserId = Convert.ToInt16(DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
    // usr.NickName = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
    // usr.Name = ((TextBox)e.Item.Cells[3].Controls[0]).Text;

     DataGrid1.EditItemIndex = -1;

     DetailUser1.GetData();
    
     usr.NickName=DetailUser1.NickName;
     usr.Name=DetailUser1.Name;
     usr.Email=DetailUser1.Email;
     usr.Country=DetailUser1.Country;
     usr.City=DetailUser1.City;
     usr.BirthDay= DetailUser1.BirthDay;
     usr.Password=Jungle.Blog.UserManager.getMd5Hash(DetailUser1.Password);
     usr.Vip=DetailUser1.vip;
     usr.Avatar=DetailUser1.ImageUrl;
     usr.RegDate=DetailUser1.RegDate;
     usr.ICQ=DetailUser1.ICQ;
     usr.RaitingZa=DetailUser1.RaitingZa;
     usr.RaitingNet=DetailUser1.RaitingNet;
     usr.Activation=DetailUser1.Activation;
     usr.Points=DetailUser1.Points;
     usr.UserSignature=DetailUser1.UserSignature;
     usr.Update();
     Label1.Text = usr.err;
  
       Button1.Visible = true; 
       Button3.Visible = true;
     bindData();
     DataGrid1.SelectedIndex = e.Item.ItemIndex;
    }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid2_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        DataGrid2.EditItemIndex = -1;

        ForumCategory fc = new ForumCategory();
                 
        fc.r_UserRolesID = Convert.ToInt16(DataGrid2.DataKeys[e.Item.ItemIndex].ToString());

       // fc.r_ForumCategoryID = Convert.ToInt32(((TextBox)e.Item.Cells[1].Controls[0]).Text);
        DropDownList dl = ((DropDownList)e.Item.FindControl("DropDownList2"));
        fc.r_ForumCategoryID = Convert.ToInt16(dl.SelectedItem.Value);

        DropDownList dl1 = ((DropDownList)e.Item.FindControl("DropDownList3"));
        fc.r_ForumVirtID = Convert.ToInt16(dl1.SelectedItem.Value);
       // fc.r_ForumVirtID = Convert.ToInt32(((TextBox)e.Item.Cells[2].Controls[0]).Text);

        DropDownList dl2 = ((DropDownList)e.Item.FindControl("DropDownList1"));
        fc.r_RoleValue = Convert.ToInt16(dl2.SelectedItem.Value);
      
        fc.r_Update();
           
        Button2.Visible = true;
        Button4.Visible = true;
        bindData1();
        
    }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid3_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        DataGrid3.EditItemIndex = -1;

        Post bp = new Post();
        bp.r_UserRolesID = Convert.ToInt16(DataGrid3.DataKeys[e.Item.ItemIndex].ToString());
     
        DropDownList dl2 = ((DropDownList)e.Item.FindControl("DropDownList4"));
        bp.r_RoleValue = Convert.ToInt16(dl2.SelectedItem.Value);

        bp.r_Update();
              
        Button5.Visible = true;
        Button6.Visible = true;
        
        bindData2();
    }
    //--------------------------------------------------------------------------------------------------------
    protected void Button1_Click(object sender, EventArgs e)
    {
        UserManager usr = new UserManager();
        DetailUser1.GetData();

        usr.NickName = DetailUser1.NickName;
        usr.Name = DetailUser1.Name;
        usr.Email = DetailUser1.Email;
        
        usr.Country = DetailUser1.Country;
        usr.City = DetailUser1.City;
        usr.BirthDay = DetailUser1.BirthDay;
        usr.Password = Jungle.Blog.UserManager.getMd5Hash(DetailUser1.Password);
       // usr.Vip = DetailUser1.vip;
        if (DetailUser1.ImageUrl.Length > 1)
            usr.Avatar = DetailUser1.ImageUrl;
        else
            usr.Avatar = "~/UserFiles/Images/Avatars/def.jpg";
        usr.RegDate = DetailUser1.RegDate;
        usr.ICQ = DetailUser1.ICQ;
        //usr.RaitingZa = DetailUser1.RaitingZa;
        //usr.RaitingNet = DetailUser1.RaitingNet;
        usr.Activation = DetailUser1.Activation;
        usr.Points = DetailUser1.Points;
        usr.UserSignature = DetailUser1.UserSignature;

        if ((usr.NickName.Trim() != "") && (usr.Email.Trim() != ""))
        {
            DetailUser1.ClearErr();
            usr.Add();
            Label1.Text = usr.err;
        }
        else
        {
            DetailUser1.NickName_err = "Не указан";
            DetailUser1.Email_err = "Не указан";
            DetailUser1.SetErr();
        }
      
        bindData();
    }
    //--------------------------------------------------------------------------------------------------------
    protected void Button2_Click(object sender, EventArgs e)
    {
        ForumCategory fc = new ForumCategory();

        fc.r_RoleValue = Convert.ToInt32(DropDownDRoles.SelectedItem.Value);
        fc.r_ForumCategoryID = Convert.ToInt32(DropDownDForumCategory.SelectedItem.Value);
        fc.r_ForumVirtID = Convert.ToInt32(DropDownDForum.SelectedItem.Value);
        //if (Session["uid"] != null) fc.r_UserID = Convert.ToInt32(Session["uid"]);
        fc.r_UserID = Convert.ToInt32(lb_userId.Text);

            fc.r_Add();
            Label1.Text = fc.err;
       
        bindData1();
    }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
      DataGrid1.EditItemIndex = -1;
        Button1.Visible = true; 
        Button3.Visible = true;
       bindData();
       DataGrid1.SelectedIndex = e.Item.ItemIndex;
    }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid2_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        DataGrid2.EditItemIndex = -1;
          Button2.Visible = true;
          Button4.Visible = true;
        bindData1();
    }
    //--------------------------------------------------------------------------------------------------------
    private void DataGrid3_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        DataGrid3.EditItemIndex = -1;
        Button5.Visible = true;
        Button6.Visible = true;
        bindData2();
    }
     //--------------------------------------------------------------------------------------------------------
     protected void Item_Command(object sender, DataGridCommandEventArgs e)
     {
             if ((e.CommandName.ToString()=="Select") || (e.CommandName.ToString()=="Edit"))
            
             {
                 if (e.CommandName.ToString() == "Edit") { Button1.Visible = false; Button3.Visible = false; DetailUser1.ShowPass(); }
                 if (e.CommandName.ToString() == "Select") { Button1.Visible = true; Button3.Visible = true; DetailUser1.HidePass(); }
                 
                 UserManager usr = new UserManager();
                 usr.UserId = Convert.ToInt16(DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
                 lb_userId.Text = usr.UserId.ToString();

                 //Session["uid"] = US_ID;
                 usr.GetById();
                 Label18.Text = usr.NickName;
                 Label27.Text = usr.NickName;
                 NickName = usr.NickName;
                
                     DetailUser1.UserId = usr.UserId;
                     DetailUser1.NickName = usr.NickName;
                     DetailUser1.Name = usr.Name;
                     DetailUser1.Email = usr.Email;
                     DetailUser1.Country = usr.Country;
                     DetailUser1.City = usr.City;
                     DetailUser1.BirthDay = usr.BirthDay;
                     DetailUser1.Password = usr.Password;
                     DetailUser1.vip = usr.Vip;
                     DetailUser1.ImageUrl = usr.Avatar;
                     DetailUser1.RegDate = usr.RegDate;
                     DetailUser1.ICQ = usr.ICQ;
                     DetailUser1.RaitingZa = usr.RaitingZa;
                     DetailUser1.RaitingNet = usr.RaitingNet;
                     DetailUser1.Activation = usr.Activation;
                     DetailUser1.Points = usr.Points;
                     DetailUser1.UserSignature = usr.UserSignature;

                     DetailUser1.SetData();
        
                 bindData1();
                 bindData2();
             }
     }
     //----------------------------------------------------------------------------------------------------
     // переход в гриде по страничкам
     //----------------------------------------------------------------------------------------------------
     void DataGrid1_Change(Object sender, DataGridPageChangedEventArgs e)
     {
         // For the DataGrid control to navigate to the correct page when paging is
         // allowed, the CurrentPageIndex property must be programmatically updated.
         // This process is usually accomplished in the event-handling method for the
         // PageIndexChanged event.

         // Set CurrentPageIndex to the page the user clicked.
         if (e.NewPageIndex == null) DataGrid1.CurrentPageIndex = 1;
         else
         DataGrid1.CurrentPageIndex = e.NewPageIndex;

         // Re-bind the data to refresh the DataGrid control. 
         bindData();
     }
     //----------------------------------------------------------------------------------------------------
     // переход в гриде по страничкам
     //----------------------------------------------------------------------------------------------------
     void DataGrid2_Change(Object sender, DataGridPageChangedEventArgs e)
     {
         // For the DataGrid control to navigate to the correct page when paging is
         // allowed, the CurrentPageIndex property must be programmatically updated.
         // This process is usually accomplished in the event-handling method for the
         // PageIndexChanged event.

         // Set CurrentPageIndex to the page the user clicked.
         DataGrid2.CurrentPageIndex = e.NewPageIndex;

         // Re-bind the data to refresh the DataGrid control. 
         bindData1();
     }
     //----------------------------------------------------------------------------------------------------
     // переход в гриде по страничкам
     //----------------------------------------------------------------------------------------------------
     void DataGrid3_Change(Object sender, DataGridPageChangedEventArgs e)
     {
         // For the DataGrid control to navigate to the correct page when paging is
         // allowed, the CurrentPageIndex property must be programmatically updated.
         // This process is usually accomplished in the event-handling method for the
         // PageIndexChanged event.

         // Set CurrentPageIndex to the page the user clicked.
         DataGrid3.CurrentPageIndex = e.NewPageIndex;

         // Re-bind the data to refresh the DataGrid control. 
         bindData2();
     }
   
     //----------------------------------------------------------------------------------------------------
     private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
     {
         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
             ((Button)e.Item.Cells[4].Controls[0]).Attributes.Add("onclick", "return confirm('Вы уверены ?');");
     }
     //----------------------------------------------------------------------------------------------------
     private void DataGrid2_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
     {
         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
             ((Button)e.Item.Cells[4].Controls[0]).Attributes.Add("onclick", "return confirm('Вы уверены ?');");

       
         DropDownList ddlOrder2 = ((DropDownList)e.Item.FindControl("DropDownList1"));
         DropDownList ddlOrder = ((DropDownList)e.Item.FindControl("DropDownList2"));
         DropDownList ddlOrder1 = ((DropDownList)e.Item.FindControl("DropDownList3"));

         switch (e.Item.ItemType)
         {
             case ListItemType.EditItem:

                ForumCategory fc = new Jungle.Forum.ForumCategory();
                 DataSet ds = new DataSet();
                 ds = fc.GetAllCategory();
                       
                 ddlOrder.Items.Clear();
                 ddlOrder.DataSource = ds.Tables[0].DefaultView;
                 ddlOrder.DataTextField = "title";        //(то, что видит юзер)
                 ddlOrder.DataValueField = "ForumCategoryID";      //(value значения в <option>) 
                 ddlOrder.DataBind();

                 ListItem NewItemZ = new ListItem();
                 NewItemZ.Text = "";
                 NewItemZ.Value = "-1";
                 ddlOrder.Items.Add(NewItemZ);
                

                //----------
              ForumVirt fv = new Jungle.Forum.ForumVirt();
                DataSet ds1 = new DataSet();
                fv.GetAllForumVirt();
                ds1 = fv.ds;
               
                ddlOrder1.Items.Clear();
                ddlOrder1.DataSource = ds1.Tables[0].DefaultView;
                ddlOrder1.DataTextField = "title";        //(то, что видит юзер)
                ddlOrder1.DataValueField = "ForumId";      //(value значения в <option>) 
                ddlOrder1.DataBind();

               ListItem NewItem = new ListItem();
               NewItem.Text = "";
               NewItem.Value = "-1";
               ddlOrder1.Items.Add(NewItem);
          

                //----------
                UserManager um = new Jungle.Blog.UserManager();
                DataSet ds2 = new DataSet();
                ds2 = um.GetRoles();
               
                ddlOrder2.Items.Clear();
                ddlOrder2.DataSource = ds2.Tables[0].DefaultView;
                ddlOrder2.DataTextField = "descript";        //(то, что видит юзер)
                ddlOrder2.DataValueField = "RoleID";      //(value значения в <option>) 
                ddlOrder2.DataBind();

                ListItem NewItemU = new ListItem();
                NewItemU.Text = "";
                NewItemU.Value = "-1";
                ddlOrder2.Items.Add(NewItemU);
         
                break;

        } //end switch


        // находим в DDL нужное 
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
         {
            switch ((int)((DataRowView)e.Item.DataItem)["RoleValue"])
            {
                case 1: e.Item.Cells[0].BackColor = Color.Gold; //e.Item.BackColor = Color.Gold; //вся строка
                        e.Item.Cells[0].ToolTip = "Администратор";
                    break;
                case 2: e.Item.Cells[0].BackColor = Color.LightCoral;
                    e.Item.Cells[0].ToolTip = "Модератор";
                    break;
                case 3: e.Item.Cells[0].BackColor = Color.LightBlue;
                    e.Item.Cells[0].ToolTip = "Пользователь";
                    break;
            }
            
            DataRowView drv = (DataRowView)e.Item.DataItem;

           String frmcid = drv["ForumCategoryID"].ToString();
            // используем не видимый контрол 
            Label l2 = (Label)e.Item.FindControl("Label22");
            DropDownForumCategory.SelectedValue = frmcid;
            l2.Text = DropDownForumCategory.SelectedItem.Text;
            DropDownForumCategory.SelectedIndex = -1;
         

            String frmtid = drv["ForumVirtId"].ToString();
            // используем не видимый контрол 
            Label l3 = (Label)e.Item.FindControl("Label23");
            DropDownForum.SelectedValue = frmtid;
            l3.Text = DropDownForum.SelectedItem.Text;
            DropDownForum.SelectedIndex = -1;
          
          
                int rid = (int)((DataRowView)e.Item.DataItem)["RoleValue"];
                // используем не видимый контрол 
                Label l1 = (Label)e.Item.FindControl("LabelR");
                DropDownUsRoles.SelectedValue = Convert.ToString(rid);
                l1.Text = DropDownUsRoles.SelectedItem.Text;
                DropDownUsRoles.SelectedIndex = -1;
          
        }

        // находим в DDL нужное 
        if (e.Item.ItemType == ListItemType.EditItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
         
            String curfrm = drv["ForumCategoryId"].ToString();
            DropDownList ddl =
                (DropDownList)e.Item.FindControl("DropDownList2");
            ddl.SelectedIndex =
                ddl.Items.IndexOf(ddl.Items.FindByValue(curfrm));
            
            //---------------
     
          String curt = drv["ForumVirtId"].ToString();
          DropDownList dd2 =
               (DropDownList)e.Item.FindControl("DropDownList3");
          dd2.SelectedIndex =
              dd2.Items.IndexOf(dd2.Items.FindByValue(curt));
          
            //---------------
      
            String ur = drv["RoleValue"].ToString();
            DropDownList dd3 =
                (DropDownList)e.Item.FindControl("DropDownList1");
            dd3.SelectedIndex =
                dd3.Items.IndexOf(dd3.Items.FindByValue(ur));
       
        }
    }
    //----------------------------------------------------------------------------------------------------
    protected void Button3_Click(object sender, EventArgs e)
    {
        DetailUser1.ClearData(); 
        DetailUser1.ClearErr();
    }
    //----------------------------------------------------------------------------------------------------
    protected void Button4_Click(object sender, EventArgs e)
    {
       fillDropDown();

        DropDownDRoles.SelectedValue = "-1"; 
        DropDownDForum.SelectedValue = "-1"; 
        DropDownDForum.SelectedValue = "-1";
    }
    //----------------------------------------------------------------------------------------------------
    private void DataGrid3_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            ((Button)e.Item.Cells[2].Controls[0]).Attributes.Add("onclick", "return confirm('Вы уверены ?');");


        DropDownList ddlOrder = ((DropDownList)e.Item.FindControl("DropDownList4"));
       
        switch (e.Item.ItemType)
        {
            case ListItemType.EditItem:

                  //----------
                UserManager um = new Jungle.Blog.UserManager();
                DataSet ds2 = new DataSet();
                ds2 = um.GetRoles();

                ddlOrder.Items.Clear();
                ddlOrder.DataSource = ds2.Tables[0].DefaultView;
                ddlOrder.DataTextField = "descript";        //(то, что видит юзер)
                ddlOrder.DataValueField = "RoleID";      //(value значения в <option>) 
                ddlOrder.DataBind();

                ListItem NewItemU = new ListItem();
                NewItemU.Text = "";
                NewItemU.Value = "-1";
                ddlOrder.Items.Add(NewItemU);

                break;

        } //end switch


        // находим в DDL нужное 
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            switch ((int)((DataRowView)e.Item.DataItem)["RoleValue"])
            {
                case 1: e.Item.Cells[0].BackColor = Color.Gold; //e.Item.BackColor = Color.Gold; //вся строка
                    e.Item.Cells[0].ToolTip = "Администратор";
                    break;
                case 2: e.Item.Cells[0].BackColor = Color.LightCoral;
                    e.Item.Cells[0].ToolTip = "Модератор";
                    break;
                case 3: e.Item.Cells[0].BackColor = Color.LightBlue;
                    e.Item.Cells[0].ToolTip = "Пользователь";
                    break;
            }

            DataRowView drv = (DataRowView)e.Item.DataItem;

            int rid = (int)((DataRowView)e.Item.DataItem)["RoleValue"];
            // используем не видимый контрол 
            Label l1 = (Label)e.Item.FindControl("Label24");
            DropDownUsRoles1.SelectedValue = Convert.ToString(rid);
            l1.Text = DropDownUsRoles1.SelectedItem.Text;
            DropDownUsRoles1.SelectedIndex = -1;

        }

        // находим в DDL нужное 
        if (e.Item.ItemType == ListItemType.EditItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;

            String ur = drv["RoleValue"].ToString();
            DropDownList dd3 =
                (DropDownList)e.Item.FindControl("DropDownList4");
            dd3.SelectedIndex =
                dd3.Items.IndexOf(dd3.Items.FindByValue(ur));

        }   
    }
    //--------------------------------------------------------------------------------------------------------
    protected void Button5_Click(object sender, EventArgs e)
    {
        fillDropDown1();
        DropDownDRoles.SelectedValue = "-1"; 
    }
    //--------------------------------------------------------------------------------------------------------
    protected void Button6_Click(object sender, EventArgs e)
    {
        Post bp = new Post();

        bp.r_RoleValue = Convert.ToInt32(DropDownDRoles1.SelectedItem.Value);
        //if(Session["uid"] != null) bp.r_UserID = Convert.ToInt32(Session["uid"]);
        bp.r_UserID = Convert.ToInt32(lb_userId.Text);
     
        bp.r_Add();
        Label6.Text = bp.err;

        DataGrid3.EditItemIndex = -1;
        bindData2();
    }
    //--------------------------------------------------------------------------------------------------------
    public override void FillBaseInfo()
    {
     sysName = "UserManager";
    }
    //--------------------------------------------------------------------------------------------------------
    public override void Main()
    {
    }

 }

